function checkdata() {
	if (!checkName(categoryadd.name.value)) return false;
	if (!checkDescr(categoryadd.descr.value)) return false;
	if(!checkInprice(categoryadd.inprice.value)) return false;
	if(!checkOutprice(categoryadd.outprice.value)) return false;
	if(!checkInven(categoryadd.inven.value)) return false;
	return true;
}

function checkName(sn){
	if( sn.length<3 || sn.length>40 ) {
		//alert("\请输入正确的名字,名字长度为3～40位！");
		document.getElementById("nameErr").innerHTML = "<font color = 'red'>Name's length is 3 to 40！</font>";
		categoryadd.name.focus();
		return false;
	}
	document.getElementById("nameErr").innerHTML = "";
	return true;
}

function checkDescr(cd){
	if(  cd.length>255 ) {
		//alert("\描述长度不能大于255個字符！");
		document.getElementById("descriptionErr").innerHTML = "<font color = 'red'>Length cannot exceed 255 characters！</font>";
		categoryadd.descr.focus();
		return false;
	}
	return true;
}

function checkInprice(ci){
	if( ci == "" ) {
		//alert("\请输入正确的Cost,Cost不能為空！");
		document.getElementById("inpriceErr").innerHTML = "<font color = 'red'>Please input the Cost！</font>";
		categoryadd.inprice.focus();
		return false;
	}
	if (isWhiteWpace(ci)){
		//alert("\请输入正确的Cost,Cost中不能包含空格！");
		document.getElementById("inpriceErr").innerHTML = "<font color = 'red'>Cost cannot contains space！</font>";
		categoryadd.inprice.focus();
		return false;
	}
	for(a = 0;a<ci.length;a++) {
		if((ci.charAt(a) > '9' || ci.charAt(a) <'0' )&& ci.charAt(a) != '.') {
			//alert("\请输入正确的Cost金額！");
			document.getElementById("inpriceErr").innerHTML = "<font color = 'red'>Please enter the correct amount of Cost！</font>";
			categoryadd.inprice.focus();
			return false;
		}
	}
	document.getElementById("inpriceErr").innerHTML = "";
	return true;
}

function checkOutprice(co){
	if( co == "" ) {
		//alert("\请输入正确的Price,Price不能為空！");
		document.getElementById("outpriceErr").innerHTML = "<font color = 'red'>Please input the Price！</font>";
		categoryadd.outprice.focus();
		return false;
	}
	if (isWhiteWpace(co)){
		//alert("\请输入正确的Price,Price中不能包含空格！");
		document.getElementById("outpriceErr").innerHTML = "<font color = 'red'>Price cannot contains space！</font>";
		categoryadd.outprice.focus();
		return false;
	}
	for(a = 0;a<co.length;a++) {
		if((co.charAt(a) > '9' || co.charAt(a) <'0' )&& co.charAt(a) != '.') {
			//alert("\请输入正确的Pricec金額！");
			document.getElementById("outpriceErr").innerHTML = "<font color = 'red'>Please enter the correct amount of Pricec！</font>";
			categoryadd.outprice.focus();
			return false;
		}
	}
	document.getElementById("outpriceErr").innerHTML = "";
	return true;
}

function checkInven(ci){
	if( ci == "" ) {
		//alert("\请输入正确的Inventory,Inventory不能為空！");
		document.getElementById("inventoryErr").innerHTML = "<font color = 'red'>Please input the Inventory！</font>";
		categoryadd.inven.focus();
		return false;
	}
	if (isWhiteWpace(ci)){
		//alert("\请输入正确的Inventory,Inventory中不能包含空格！");
		document.getElementById("inventoryErr").innerHTML = "<font color = 'red'>Inventory cannot contains space！</font>";
		categoryadd.inven.focus();
		return false;
	}
	for(a = 0;a<ci.length;a++) {
		if(ci.charAt(a) > '9' || ci.charAt(a) <'0' ) {
			//alert("\请输入正确的Inventory,Inventory中只能為數字！");
			document.getElementById("inventoryErr").innerHTML = "<font color = 'red'>Inventory must be number！</font>";
			categoryadd.inven.focus();
			return false;
		}
	}
	document.getElementById("inventoryErr").innerHTML = "";
	return true;
}

function strlen(str){
	var len;
	var i;
	len = 0;
	for (i=0;i<str.length;i++){
		if (str.charCodeAt(i)>255) len+=2; else len++;
	}
	return len;
}
function strlen2(str){
	var len;
	var i;
	len = 0;
	for (i=0;i<str.length;i++){
		if (str.charCodeAt(i)>255) return true;
	}
	return false;
}
function isWhiteWpace (s)
{
  var whitespace = " \t\n\r";
  var i;
  for (i = 0; i < s.length; i++){   
     var c = s.charAt(i);
     if (whitespace.indexOf(c) >= 0) {
		  return true;
	  }
   }
   return false;
}
function isSsnString (ssn)
{
	var re=/^[0-9a-z][\w-.]*[0-9a-z]$/i;
	if(re.test(ssn))
		return true;
	else
		return false;
}
/*
 * function checkssn(gotoURL) { var
 * ssn=categoryadd.username.value.toLowerCase(); if (checkUserName(ssn)){ var
 * open_url = gotoURL + "?username=" + ssn;
 * window.open(open_url,'','status=0,directories=0,resizable=0,toolbar=0,location=0,scrollbars=0,width=390,height=260'); } }
 */
